﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IDML.BusinessObject;
using IDML.BusinessLogic;
using System.Data;

namespace IDML.Web
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PopulateMenus();
            }
            if (Request.Params["Sel"] != null)
            {
                if (Request.Params["Sel"].ToString() != "")
                {
                    // logger.Error("Sel Value:" + Request.Params["Sel"].ToString());

                    Response.Redirect(Request.Params["Sel"].ToString());

                }

            }

        }

        private void PopulateMenus()
        {
            try
            {
                BindNullDataSource();
                xmlDataSource.Data = null;
                UserBL ubl = new UserBL();
                IDMLSession idmlSession = new IDMLSession();

                if (idmlSession.User != null)
                {
                    int RoleId = idmlSession.User.RoleId;
                    DataSet ds = ubl.GetUserMenuList(RoleId, true);
                    if (ds != null && ds.Tables[0].Rows.Count > 0)
                    {
                        ds.DataSetName = "Menus";
                        ds.Tables[0].TableName = "Menu";
                        DataRelation relation = new DataRelation("ParentChild",
                                ds.Tables["Menu"].Columns["MenuId"],
                                ds.Tables["Menu"].Columns["ParentId"],
                                true);
                        relation.Nested = true;
                        ds.Relations.Add(relation);
                        xmlDataSource.Data = ds.GetXml();
                    }
                    else
                    {
                        Response.Redirect("Login.aspx");
                        //logger.Error("Mastepage redirect To login Page");
                    }
                }
                else
                {

                }
            }
            catch (Exception ex)
            {
                // logger.Error(ex.Message);
                Response.Redirect("Login.aspx");
            }
        }

        private void BindNullDataSource()
        {
            try
            {
                Menu1.Items.Clear();
            }
            catch (Exception ex)
            {
                // logger.Error("BindNullDataSource()" + ex.Message);
                throw ex;
            }

        }
    }
}
